package com.esunrising.esungsc.common.utils;

import java.io.Serializable;
import java.util.UUID;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class CustomUUIDGenerator implements IdentifierGenerator {
    public static final String NAME = "customUUID";
    public static final String TYPE = "com.esunrising.esungsc.common.utils.CustomUUIDGenerator";
    private static final Logger logger = LoggerFactory.getLogger(CustomUUIDGenerator.class);

    /**
     * 生成一个唯一的 ID。
     *
     * @param sharedSessionContractImplementor Hibernate 会话实现
     * @param o                                对象
     * @return 生成的唯一 ID
     * @throws HibernateException 如果生成过程中出现异常
     */
    @Override
    public Serializable generate(SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException {
        try {
            String uniqueId = UUID.randomUUID().toString().replace("-","").toUpperCase();
            logger.info("Generated unique ID: {}", uniqueId);
            return uniqueId;
        } catch (Exception e) {
            logger.error("Error generating unique ID", e);
            throw new HibernateException("Failed to generate unique ID", e);
        }
    }
}
